﻿using CG5_KT.Primitives;

namespace CG5_KT.Scene
{
    public struct Face
    {
        // A (B and C) is in fact index of vertex in the mesh's vertices list.
        // Mesh vertex stores actual coordinates, etc.
        public int A;
        public int B;
        public int C;
    }

    public struct Vertex
    {
        //TODO: Vector4 for affine?
        public Vector3 Normal;
        public Vector3 Coordinates;
        public Vector3 WorldCoordinates;
    }

    public struct ScanLineData
    {
        public int currentY;
        public float ndotla;
        public float ndotlb;
        public float ndotlc;
        public float ndotld;
    }

    public class Mesh
    {
        public string Name { get; set; }
        public Vertex[] Vertices { get; private set; }
        public Face[] Faces { get; set; }
        public Vector3 Position { get; set; }
        public Vector3 Rotation { get; set; }
        public Color4 SurfaceColor { get; set; }

        public Mesh(string name, int verticesCount, int facesCount)
        {
            this.Vertices = new Vertex[verticesCount];
            this.Faces = new Face[facesCount];
            this.Name = name;
            this.SurfaceColor = new Color4(0.95f);
        }

        public void Scale(Vector3 scale)
        {
            for (int i = 0; i < this.Vertices.Length; i++)
            {
                this.Vertices[i].Coordinates.X *= scale.X;
                this.Vertices[i].Coordinates.Y *= scale.Y;
                this.Vertices[i].Coordinates.Z *= scale.Z;
            }
        }

        public void TranslateVertices(Vector3 trans)
        {
            // Note that only vertices are translated, mesh central point (origin)
            // remains untouched. This is useful for rotation in case mesh is part of
            // a model (e.g. it is a wing of an aircraft) and it is to rotated around
            // origin of the aircraft (not wing's origin).

            for (int i = 0; i < this.Vertices.Length; i++)
            {
                this.Vertices[i].Coordinates.X += trans.X;
                this.Vertices[i].Coordinates.Y += trans.Y;
                this.Vertices[i].Coordinates.Z += trans.Z;
            }
        }
    }
}
